﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using IFRS.Models;

namespace IFRS.DataAccess.DAL
{
    public class SPR_GLACCOUNT
    {
        public DataTable SEARCH_ALL(GLAccount glAccount, Paging paging, bool isAllBranch, string sCostCenter, ref int iRecordCount)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex.AsDBValue()));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize.AsDBValue()));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting.AsDBValue()));

                lstParams.Add(new SqlParameter("@isAllBranch", isAllBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@sCostCenter", sCostCenter.AsDBValue()));

                lstParams.Add(new SqlParameter("@GLAccountCode", glAccount.GLAccountCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccountName", glAccount.GLAccountName.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsActive", glAccount.IsActive.AsDBValue()));

                dt = utility.StoredProcDataTable("SPR_GLACCOUNT_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int glAccountId)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@GLAccountID", glAccountId));
                dt = utility.StoredProcDataTable("SPR_GLACCOUNT_SEARCHBYID", lstParams.ToArray());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(GLAccount glAccount)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@GLAccountID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;

                lstParams.Add(new SqlParameter("@PrevGLAccountCode", glAccount.PrevGLAccountCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccountCode", glAccount.GLAccountCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@PrevGLAccountName", glAccount.PrevGLAccountName.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccountName", glAccount.GLAccountName.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsActive", glAccount.IsActive.AsDBValue()));

                lstParams.Add(new SqlParameter("@CreatedDate", glAccount.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", glAccount.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", glAccount.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", glAccount.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", glAccount.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", glAccount.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", glAccount.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", glAccount.UpdatedBranch.AsDBValue()));

                utility.ExecuteStoredProc("SPR_GLACCOUNT_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(GLAccount glAccount)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@GLAccountID", glAccount.GLAccountID.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccountCode", glAccount.GLAccountCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccountName", glAccount.GLAccountName.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsActive", glAccount.IsActive.AsDBValue()));

                lstParams.Add(new SqlParameter("@UpdatedDate", glAccount.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", glAccount.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", glAccount.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", glAccount.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_GLACCOUNT_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(GLAccount glAccount)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@GLAccountID", glAccount.GLAccountID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", glAccount.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", glAccount.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", glAccount.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", glAccount.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_GLACCOUNT_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}
